package cn.edu.swu.toy;

import cn.edu.swu.core.AbstractRepo;
import cn.edu.swu.core.ResultSetVisitor;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class ToyRepo extends AbstractRepo {
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");

    public List<Toy> getAllToys() throws SQLException, ClassNotFoundException {
        List<Toy> toys = new ArrayList<>();
        String sql = "SELECT * FROM toy";
        this.query(sql, new ResultSetVisitor() {
            @Override
            public void visit(ResultSet rs) throws SQLException {
                toys.add(getOneRecord(rs));
            }
        });
        return toys;
    }

    public void save(Toy toy) throws SQLException {
        if (toy.getId() < 0) {
            this.add(toy);
        } else {
            this.update(toy);
        }
    }

    public void add(Toy toy) throws SQLException {
        String template = "INSERT INTO toy (name, brand, price, memo, produce, image) " +
                "values ('%s', '%s', %s, '%s', '%s', '%s')";
        this.execute(String.format(template, toy.getName(), toy.getBrand(), toy.getPrice(),
                toy.getMemo(), dateFormat.format(toy.getProduce()), toy.getImage()));
    }

    public void update(Toy toy) throws SQLException {
        String template = "UPDATE toy " +
                "SET name='%s', brand='%s', price=%s, memo='%s', produce='%s', image='%s' " +
                "WHERE id=%s";
        this.execute(String.format(template, toy.getName(), toy.getBrand(), toy.getPrice(),
                toy.getMemo(), dateFormat.format(toy.getProduce()), toy.getImage(), toy.getId()));
    }

    public void deleteById(int id) throws SQLException {
        String template = "DELETE FROM toy WHERE id = %s";
        this.execute(String.format(template, id));
    }

    public Toy getById(int id) throws SQLException {
        List<Toy> toys = new ArrayList<>();
        String sql = "SELECT * FROM toy WHERE id=" + id;
        this.query(sql, new ResultSetVisitor() {
            @Override
            public void visit(ResultSet rs) throws SQLException {
                toys.add(getOneRecord(rs));
            }
        });
        return toys.size() > 0 ? toys.get(0) : null;
    }

    public List<Toy> search(String key) throws SQLException {
        List<Toy> toys = new ArrayList<>();
        String sql = "SELECT * FROM toy WHERE name LIKE '%" + key + "%' " +
                "OR brand LIKE '%" + key + "%'";
        this.query(sql, new ResultSetVisitor() {
            @Override
            public void visit(ResultSet rs) throws SQLException {
                toys.add(getOneRecord(rs));
            }
        });
        return toys;
    }

    private Toy getOneRecord(ResultSet rs) throws SQLException {
        Toy toy = new Toy();
        toy.setId(rs.getInt("id"));
        toy.setName(rs.getString("name"));
        toy.setBrand(rs.getString("brand"));
        toy.setPrice(rs.getDouble("price"));
        toy.setMemo(rs.getString("memo"));
        toy.setProduce(rs.getDate("produce"));
        toy.setImage(rs.getString("image"));
        return toy;
    }



}
